<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="flag = true"
          >添加分类</el-button
        >
      </div>
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="200">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别号"> </el-table-column>
        <el-table-column label="操作" width="300px"
          ><template v-slot="scope">
            <el-button type="primary" size="small" @click="doEdit(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="doDel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="dialogText.text" :visible.sync="flag" @close="doClose">
      <el-form
        label-width="80px"
        :model="cateForm"
        :rules="cateFormRules"
        ref="cateFormRef"
      >
        <el-form-item label="分类文章" prop="cate_alias">
          <el-input v-model="cateForm.cate_alias"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_name">
          <el-input v-model="cateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label-width="70%">
          <el-button type="info" @click="doReset">取消</el-button>
          <el-button type="primary" @click="doAddCate">{{
            dialogText.btn
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  // 1.数据
  data() {
    return {
      //1.1分类列表
      list: [],
      //1.2dialog对话框布尔属性
      flag: false,
      //1.3表单数据
      cateForm: {
        cate_alias: "",
        cate_name: "",
      },
      //1.4表单校验
      cateFormRules: {
        cate_alias: [
          { required: true, message: "请输入分类别号", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
      },
      //1.5dialog复用数据
      dialogText: {
        text: "添加文章分类",
        btn: "确定",
      },
    };
  },
  //2.方法
  methods: {
    //2.1查询分类列表
    async getCateList() {
      const { data: res } = await this.$axios.get("/my/cate/list");
      //2.2将数据存入data中
      this.list = res.data;
    },
    //2.2新增文章分类
    doAddCate() {
      this.$refs.cateFormRef.validate(async (valid) => {
        if (valid) {
          //判断是新增还是编辑
          let promise = null;
          if (this.dialogText.btn === "确定") {
            promise = await this.$axios.post("/my/cate/add", this.cateForm);
          } else {
            promise = await this.$axios.put("/my/cate/info", this.cateForm);
          }

          const { data: res } = promise;
          this.$message.success(res.message);
          //成功 : 弹窗消失+刷新列表+清空表单
          if (res.code === 0) {
            this.getCateList();
            this.flag = false;
            this.cateForm.cate_name = "";
            this.cateForm.cate_alias = "";
          }
        }
      });
    },
    //2.3取消按钮
    doReset() {
      this.$refs.cateFormRef.resetFields();
    },
    //2.4编辑按钮
    doEdit(row) {
      //(1)修改文本
      this.dialogText = {
        text: "编辑文章分类",
        btn: "编辑",
      };
      this.flag = true;
      //(2)设置默认内容
      this.cateForm = { ...row };
    },
    //2.5对话框消失,回复默认状态
    doClose() {
      this.dialogText = {
        text: "添加文章分类",
        btn: "确定",
      };
      //清空表单
      this.cateForm.cate_name = "";
      this.cateForm.cate_alias = "";
    },
    //2.6删除按钮
    async doDel(id) {
      const { data: res } = await this.$axios.delete("/my/cate/del", {
        params: { id },
      });
      if (res.code === 0) {
        //更新列表+提示
        this.getCateList();
        this.$message.success(res.message);
      }
    },
  },
  //3.生命周期
  created() {
    //因为查询数据要在多个地方使用,所有封装成函数
    this.getCateList();
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
